# coding=utf-8
'''
@author: guichaochao

@desc:发送报告邮件

'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import time, os


# =============定义发送邮件==========
# SMTP服务器
SmtpServer = 'smtp.126.com'
# 发信邮箱
Sender = 'testchao@126.com'
AuthCode = 'chao123'
# 收信邮箱
Receiver = ['guicc@knowbox.cn']


def send_mail(file_new, mailto):
    # 定义邮件内容
    f = open(file_new, 'rb')
    # HTML形式内容
    mail_body = f.read()
    htmlBody = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    f.close()
    # 构造附件
    att = MIMEText(open(file_new, 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment; filename="小学学生端测试报告.html"'
    msg = MIMEMultipart('alternative')
    msg.attach(htmlBody)
    msg.attach(att)
    # msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    # 定义标题
    msg['Subject'] = Header('测试报告', 'utf-8')
    msg['From'] = Sender
    msg['To'] = ",".join(mailto)
    # 定义发送时间
    msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
    smtp = smtplib.SMTP()
    # 连接 SMTP 服务器
    smtp.connect(SmtpServer)
    # 用户名密码
    smtp.login(Sender, AuthCode)
    smtp.sendmail(Sender, mailto, msg.as_string())
    smtp.quit()
    print 'Email has send out !'


# ======查找测试报告目录，找到最新生成的测试报告文件====
def send_report(report_path):
    # result_dir = report_path
    lists = os.listdir(report_path)
    lists.sort(key=lambda fn: os.path.getmtime(report_path + "\\" + fn))
    # print (u'最新测试生成的报告： '+lists[-1])
    # 找到最新生成的文件
    file_new = os.path.join(report_path, lists[-1])
    print file_new
    # 调用发邮件模块
    send_mail(file_new, Receiver)